if SERVER then
	AddCSLuaFile( "shared.lua" )
end

if CLIENT then
	SWEP.Slot				= 3
	SWEP.SlotPos			= 1
	SWEP.DrawCrosshair      = false
	SWEP.IconLetter = ""
	SWEP.DrawAmmo = false
end

SWEP.PrintName			= "Picklock Device"
SWEP.Author				= "Jammie Dodger"
SWEP.Instructions		= "Left click to picklock a door.\n 20% chance to unlock."
	
SWEP.Spawnable			= false
SWEP.AdminSpawnable		= true

SWEP.ViewModel			= "models/weapons/v_crowbar.mdl"
SWEP.WorldModel			= "models/weapons/w_crowbar.mdl"

SWEP.Primary.Sound = "weapons/crowbar/crowbar_impact1.wav"
SWEP.Primary.ClipSize		= -1
SWEP.Primary.DefaultClip	= -1
SWEP.Primary.Automatic		= false
SWEP.Primary.Ammo			= ""
SWEP.Primary.Delay			= 1

SWEP.Secondary.ClipSize		= -1
SWEP.Secondary.DefaultClip	= -1
SWEP.Secondary.Automatic	= false
SWEP.Secondary.Ammo			= ""

function SWEP:Initialize()
		self:SetWeaponHoldType( "melee" )
end

function SWEP:PrimaryAttack()
	if ( CurTime() < self.Primary.Delay	) then return end
	self.Primary.Delay = CurTime() + 1
	
	local tr = self.Owner:EyeTrace(100)
	
	self.Weapon:EmitSound( self.Primary.Sound )
	
	if ( SERVER ) then
		if ( !tr.Entity:IsValid() ) then
			self.Weapon:SendWeaponAnim( ACT_VM_MISSCENTER )
			return
		end
		
		self.Weapon:SendWeaponAnim( ACT_VM_HITCENTER )
		
		if ( tr.Entity:IsValid() and tr.Entity:IsDoor() ) then
			self:AttemptUnlock(tr)
		end
	end
end

function SWEP:AttemptUnlock(tr)
	if tr.Entity:IsCopOnly() then self.Owner:SendNotify( "This door has been reinforced!", NOTIFY_ERROR, 4 ) return end
	if tr.Entity.Owners and tr.Entity.Owners == self.Owner:UniqueID() then self.Owner:SendNotify( "This is your door!", NOTIFY_ERROR, 4 ) return end
	if tr.Entity.CoOwners and table.HasValue(tr.Entity.CoOwners, self.Owner:UniqueID()) then self.Owner:SendNotify( "You are a coowner of this door!", NOTIFY_ERROR, 4 ) return end
	if self.Owner:Team() == 2 then self.Owner:SendNotify( "Police are unable to use picklocks!", NOTIFY_ERROR, 4 ) self.Owner:StripWeapon("ss_lockpick") return end
	if tr.Entity:IsUnownable() then tr.Entity:Fire("open") self.Owner:SendNotify( "This door is unownable!", NOTIFY_ERROR, 4 ) return end
	
	
	local RandomNumber = math.random(1,100)
	
	if RandomNumber <= 20 then
		tr.Entity:Fire("unlock")
		tr.Entity:Fire("open")
		tr.Entity.IsLocked = false
		self.Owner:EmitSound("door/latchunlocked1.wav")	
	else
		self.Owner:EmitSound("physics/metal/metal_sheet_impact_bullet1.wav")
		self.Owner:StripWeapon("ss_lockpick")
	end
end
